﻿using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;

namespace SpaceNeedle.x6473.WindowsPhone.Common
{
    public static class Extensions
    {
        public static void UpdateBinding(this TextBox textBox)
        {
            BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
            }
        }

        /// <summary>
        /// Método que força a atualização do valor de um TextBlock ao salvar
        /// </summary>
        /// <param name="textBlock">Controle desejado</param>
        public static void UpdateBinding(this TextBlock textBlock)
        {
            BindingExpression bindingExpression = textBlock.GetBindingExpression(TextBlock.TextProperty);
            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
            }
        }

        // <summary>
        /// Método que força a atualização do valor de um DatePicker ao salvar
        /// </summary>
        /// <param name="datePicker">Controle desejado</param>
        public static void UpdateBinding(this DatePicker datePicker)
        {
            BindingExpression bindingExpression = datePicker.GetBindingExpression(DatePicker.ValueProperty);
            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
            }
        }


        // <summary>
        /// Método que força a atualização do valor de um ListPicker ao salvar
        /// </summary>
        /// <param name="listPicker">Controle desejado</param>
        public static void UpdateBinding(this ListPicker listPicker)
        {
            BindingExpression bindingExpression = listPicker.GetBindingExpression(ListPicker.SelectedItemProperty);
            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
            }
        }
    }
}
